// 模型创建脚本
import { strFormat } from './utils.js'
import {Db} from './database.js'
import fs from 'fs'

export function createModel(tableName){
  return new Promise((resolve,reject)=>{
    fs.readFile('src/scripts/config.json', {encoding:'utf8'}, (err, data) => {
      if (err) throw err
      const config = JSON.parse(data)
      // db connection
      const db = new Db(config.db.host, config.db.user, config.db.password, config.db.database, config.db.port)

      const modelFileName = `${config.dbFolder}/model/${tableName}.json`
      const listFileName = `${config.dbFolder}/list/${tableName}.json`
      // const fileName = `${config.dbFolder}/${tableName}.json`

      const queryStr = strFormat(config.db.tableStrucStr, `${config.db.tablePrefix + tableName}`)
      db.query(queryStr).then((row) => {
        // const tableId = `${tableName}_id`
        const forbinddedFields = {
          company_id:1,
        }   // 不渲染为模型的字段
        const transArr = []
        const listTransArr = []
        row.map((item) => {
          if(!forbinddedFields[item.columnName]){
            item.reflectColumnName = item.columnName
            // item.isResearch = false
            // item.isKeyword = false
            // item.realatedEndTime = ""
            transArr.push(item)
            listTransArr.push({
              columnName: item.columnName,
              columnType: item.columnType,
              dataType: item.dataType,
              columnComment: item.columnComment
            })
            // listTransArr.push(Object.assign({ isResearch: false, isKeyword: false, realatedEndTime :''}))
          }
        })

        // model file
        if (fs.existsSync(modelFileName)) {
          // file is exist
          console.log(`======file is exist and ignore to created json model:${tableName}=======`)
        } else {
          // file is not exist
          if (!fs.existsSync(config.dbFolder)) {
            fs.mkdirSync(config.dbFolder, { recursive: true }, (err) => {
              if (err) throw err
            })
          }
          console.log(transArr)
          fs.writeFile(modelFileName, JSON.stringify(transArr), { flag: 'a' }, (err) => {
            if (err) {
              console.log('========error======')
              throw err
            } else {
              console.log(`======succeessfully created json model:${tableName}=======`)
            }
          })
        }

        // list file
        if (fs.existsSync(listFileName)) {
          // file is exist
          console.log(`======file is exist and ignore to created json model:${tableName}=======`)
        } else {
          // file is not exist
          if (!fs.existsSync(config.dbFolder)) {
            fs.mkdirSync(config.dbFolder, { recursive: true }, (err) => {
              if (err) throw err
            })
          }
          fs.writeFile(listFileName, JSON.stringify(listTransArr), { flag: 'a' }, (err) => {
            if (err) {
              console.log('========error======')
              throw err
            } else {
              console.log(`======succeessfully created json model:${tableName}=======`)
            }
          })
        }

        resolve(true)

      }).catch((err)=>{
        resolve(false)
        console.log(err)
      })
      db.end()
    })
  })

}